package Step;
/**
 * @author Cjj
 * 售票窗口
 * 利用多线程技术，编写一个卖票的小程序
 */
public class Station extends Thread{
     static int tick = 30; //为了线程安全，票数使用静态变量
    static Object obj = new Object();//创建一个对象，用于同步代码块
    public void ticket(){
        System.out.printf("卖出了第"+tick+"张票");
        tick--;
    }
    public  void run(){
        while(tick>0){
            synchronized (obj){
                if(tick>0){
                    ticket();
                }
            }
            if(tick==0){
                System.out.println("票卖完了");
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        //实例站台对象,并为每一个站台取名字
        Station station1 = new Station();
        Station station2 = new Station();
        Station station3 = new Station();

        //让每个站台开始工作
        station1.start();
        station2.start();
        station3.start();

    }
}
